package com.mornd.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mornd.gulimall.ware.entity.WareInfo;
import com.mornd.gulimall.ware.mapper.WareInfoMapper;
import com.mornd.gulimall.ware.service.IWareInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 仓库信息 服务实现类
 * </p>
 *
 * @author mornd
 * @since 2023-07-29
 */
@Service
public class WareInfoServiceImpl extends ServiceImpl<WareInfoMapper, WareInfo> implements IWareInfoService {
    @Override
    public IPage<WareInfo> list(WareInfo wareInfo) {
        IPage<WareInfo> page = new Page<>(wareInfo.validPageNum(), wareInfo.validPageSize());

        LambdaQueryWrapper<WareInfo> qw = Wrappers.lambdaQuery();
        String key = wareInfo.getKey();
        if (StringUtils.hasText(key)) {
            qw.like(WareInfo::getName, key)
                    .or().like(WareInfo::getAddress, key)
                    .or().like(WareInfo::getAreacode, key);
        }

        super.page(page, qw);
        return page;
    }

    @Override
    public boolean exists(Long id, String name) {
        LambdaQueryWrapper<WareInfo> qw = Wrappers.<WareInfo>lambdaQuery().eq(WareInfo::getName, name);
        qw.ne(id != null, WareInfo::getId, id);
        long count = super.count(qw);
        return count > 0;
    }
}
